package com.zdft.promotion.common.distributelock;

import com.zdft.promotion.common.service.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Configuration
public class RedisClient {
    private Logger logger = LoggerFactory.getLogger(RedisClient.class);

    @Value("${spring.redis.cluster.nodes}")
    private String host;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        if (StringUtils.isNotBlank(host)) {
            String[] hostArr = host.split(",");
            for (int i = 0; i < hostArr.length; i++) {
                hostArr[i] = "redis://" + hostArr[i];
            }
            logger.info("redisson配置情况：" + Arrays.toString(hostArr));
            config.useClusterServers().addNodeAddress(hostArr);
        } else {
            throw new BusinessException("10");
        }
        return Redisson.create(config);
    }
}
